/**
 * 
 */
package net.mikoo.seals.agent.web.interceptor;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import net.mikoo.seals.agent.common.WebContext;

/**
 * @author huangxm
 *
 */
public class BaseInterceptor implements HandlerInterceptor {

	Logger logger = Logger.getLogger(BaseInterceptor.class);

	/**
	 * 
	 */
	public BaseInterceptor() {
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		// 初始化上下文
		initWebContext(request, response);
		// 初始化分页
		initPage(request);
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";

		// 记录登陆状态
		HttpSession session = request.getSession();

		Object object = request.getAttribute("basePath");
		if (object == null) {
			session.setAttribute("basePath", basePath);
		}

		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		// TODO Auto-generated method stub

	}

	public boolean isOcclude(HttpServletRequest request, HttpServletResponse response) {

		Object accessKey = request.getSession().getAttribute("ACCESS_KEY");
		if (accessKey != null) {
			return true;
		}

		String key = request.getParameter("key");
		if (StringUtils.isNotBlank(key)) {
			if (key.equals("2016")) {
				request.getSession().setAttribute("ACCESS_KEY", key);
				return true;
			}
		}

		try {
			response.sendRedirect("/occlude.htm");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;

	}

	public void initWebContext(HttpServletRequest request, HttpServletResponse response) {

		WebContext.setRequest(request);
		WebContext.setResponse(response);
		ServletContext servletContext = request.getSession().getServletContext();
		WebContext.setServletContext(servletContext);
	}

	public void initPage(HttpServletRequest request) {

		String _page = request.getParameter("page");

		String url = request.getRequestURI();
		if (request.getQueryString() != null) {
			url += "?" + request.getQueryString();
		}
		WebContext.setUrl(url);

		Integer page = 1;
		if (_page != null) {
			try {
				page = Integer.parseInt(_page);
			} catch (Exception e) {
				logger.error("分页格式化出错！", e);
			}
		}
		WebContext.setPage(page);
	}

	public static void main(String[] args) {
		String filetext = "/wwww/aaa/bbb/ccc.htm";
		Pattern p = Pattern.compile("\\/(.*?)\\/");// 正则表达式，取q#和#q之间的字符串，不包括q#和#q
		Matcher m = p.matcher(filetext);
		m.find();

		System.out.println(m.group(1));// m.group(1)不包括这两个字符

	}

}
